//
//  UILabelExtension.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

extension UILabel {
    
    func wn_setTextTime(_ timeDouble: Double, _ frontLabel: String = "", _ backLabel: String = "") {
        var minute: Double = 0.0
        var second: Double = 0.0
        // 时间处理
        if timeDouble / 60.0 > 0.0 {
            // 超过一分钟
            minute = timeDouble / 60.0
            second = timeDouble.truncatingRemainder(dividingBy: 60.0)
        } else if timeDouble / 60.0 <= 0.0 {
            // 未超过一分钟
            minute = 0.0
            second = timeDouble
        }
        let minuteStr = String.init(format: "%02ld", Int(minute))
        let secondStr = String.init(format: "%02ld", Int(second))
        self.text = frontLabel + minuteStr + ":" + secondStr + backLabel
    }
    
    @objc func wn_bottomShadow() {
        guard let text = text else {
            return
        }
        let attrString = NSMutableAttributedString(string: text)
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.black
        shadow.shadowBlurRadius = 1
        shadow.shadowOffset = CGSize(width: 1, height: 1)
        let attribute: [NSAttributedString.Key : Any] = [.shadow: shadow]
        attrString.addAttributes(attribute, range: NSRange(location: 0, length: attrString.length))
        attributedText = attrString
    }
    
    @objc func wn_bottomShadowAndStroke() {
        guard let text = text else {
            return
        }
        let attrString = NSMutableAttributedString(string: text)
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.black
        shadow.shadowBlurRadius = 1
        shadow.shadowOffset = CGSize(width: 1, height: 1)
        let attribute: [NSAttributedString.Key : Any] = [.shadow: shadow, .strokeColor: UIColor.init(named: "blue_2090FB")!, .strokeWidth: -4]
        attrString.addAttributes(attribute, range: NSRange(location: 0, length: attrString.length))
        attributedText = attrString
    }
}

extension AVURLAsset {
    var fileSize: Int? {
        let keys: Set<URLResourceKey> = [.totalFileSizeKey, .fileSizeKey]
        let resourceValues = try? url.resourceValues(forKeys: keys)
        return resourceValues?.fileSize ?? resourceValues?.totalFileSize
    }
}
